<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
Yii::import('application.extensions.Api._base.BaseApiFunctionality');

class enquaryApiFunctionality extends BaseApiFunctionality {
    /* --------------- Start  GET Actions --------------------- */

    function getEnquaryByUserId() {
        if ($this->validGetRequest()) {


            $data = Yii::app()->db
                    ->createCommand()
                    ->select('*')
                    ->from('enquary')
                     ->andWhere("users_id=:users_id ",array(':users_id'=> $_GET['users_id']))
                    ->andWhere("enquiry_type=:enquiry_type OR enquiry_type='B'",array(':enquiry_type'=> $_GET['enquiry_type']))
	          
	
                    //->where('users_id=' . $_GET['users_id'].' AND (enquiry_type='.$_GET['enquiry_type'].' OR enquiry_type= B)')
                    ->queryAll();
            if (empty($data)) {
                $data = new ECOMResult(501, 'data not found.');
            }

            ECOMApiResponder::Respond($data, date('m-d-Y'));
        }
    }

    function getEnquaryDetailById() {

        if ($this->validGetRequest()) {
            $data = '';
            $enquary_id = $_GET['enquary_id'];
          
            if (!empty($enquary_id)) {

                $enquaries = Yii::app()->db
                        ->createCommand()
                        ->select('*')
                        ->from('enquary')
                        ->where('id=:id', array(':id' => $enquary_id))
                        ->queryRow();
            }


            if (empty($enquaries)) {
                $data = new ECOMResult(501, 'No Enqyary found. This may be a temporary error with the network. Please try back in a few minutes.');
                ECOMApiResponder::Respond($data, date('m-d-Y'));
            } else {

                ECOMApiResponder::Respond($enquaries, date('m-d-Y'));
            }
        }
    }

    /* --------------- End GET Actions ------------------------ */

    /* --------------- Start POST Actions --------------------- */

    function verifyEnquary() {
        if ($this->validPostRequest()) {

            if ((isset($_POST['enquary_id']))) {
                $enquary_id = $_POST['enquary_id'];
                $enquary = Enquary::model()->findByPk($enquary_id);

                if ($enquary) {
                    if (isset($_POST['category'])) {
                        $enquary->category = $_POST['category'];
                    }
                    if (isset($_POST['coustmer_name'])) {
                        $enquary->coustmer_name = $_POST['coustmer_name'];
                    }
                    if (isset($_POST['coustmer_id'])) {
                        $enquary->coustmer_id = $_POST['coustmer_id'];
                    }
                    if (isset($_POST['alternate_no'])) {
                        $enquary->alternate_no = $_POST['alternate_no'];
                    }

                    if (isset($_POST['installation_address'])) {
                        $enquary->installation_address = $_POST['installation_address'];
                    }
                    if (isset($_POST['city'])) {
                        $enquary->city = $_POST['city'];
                    }
                    if (isset($_POST['landmark'])) {
                        $enquary->landmark = $_POST['landmark'];
                    }
                    if (isset($_POST['zone'])) {
                        $enquary->zone = $_POST['zone'];
                    }
                    if (isset($_POST['product_type'])) {
                        $enquary->product_type = $_POST['product_type'];
                    }
                    if (isset($_POST['state'])) {
                        $enquary->state = $_POST['state'];
                    }
                    if (isset($_POST['pin_code'])) {
                        $enquary->pin_code = $_POST['pin_code'];
                    }

                    if (isset($_POST['los_no'])) {

                        $enquary->los_no = $_POST['los_no'];
                    }
                    if (isset($_POST['plan_name'])) {

                        $enquary->plan_name = $_POST['plan_name'];
                    }
                    if (isset($_POST['activation_date'])) {

                        $enquary->activation_date = $_POST['activation_date'];
                    }
                    if (isset($_POST['case_type'])) {

                        $enquary->case_type = $_POST['case_type'];
                    }
                    if (isset($_POST['address_confirmation'])) {

                        $enquary->address_confirmation = $_POST['address_confirmation'];
                    }
                    if (isset($_POST['subsriber_confirmation'])) {

                        $enquary->subsriber_confirmation = $_POST['subsriber_confirmation'];
                    }
                    if (isset($_POST['av_code'])) {

                        $enquary->av_code = $_POST['av_code'];
                    }

                    if (isset($_POST['av_result'])) {

                        $enquary->av_result = $_POST['av_result'];
                    }
                    if (isset($_POST['applied_for_connection'])) {

                        $enquary->applied_for_connection = $_POST['applied_for_connection'];
                    }

                    if (isset($_POST['name_of_person_meet'])) {

                        $enquary->name_of_person_meet = $_POST['name_of_person_meet'];
                    }

                    if (isset($_POST['relationship'])) {

                        $enquary->relationship = $_POST['relationship'];
                    }
                    if (isset($_POST['remarks'])) {

                        $enquary->remarks = $_POST['remarks'];
                    }
                    if (isset($_POST['get_location'])) {

                        $enquary->get_location = $_POST['get_location'];
                    }
                    if (isset($_POST['resident'])) {

                        $enquary->resident = $_POST['resident'];
                    }
                    if (isset($_POST['income'])) {

                        $enquary->income = $_POST['income'];
                    }

                    if (isset($_POST['organization'])) {

                        $enquary->organization = $_POST['organization'];
                    }

                    if (isset($_POST['occupation'])) {

                        $enquary->occupation = $_POST['occupation'];
                    }
                    if (isset($_POST['company'])) {

                        $enquary->company = $_POST['company'];
                    }
                    if (isset($_POST['agri_land'])) {

                        $enquary->agri_land = $_POST['agri_land'];
                    }
                    if (isset($_POST['distance_from_branch'])) {

                        $enquary->distance_from_branch = $_POST['distance_from_branch'];
                    }

                    if (isset($_POST['vehicle'])) {

                        $enquary->vehicle = $_POST['vehicle'];
                    }
                    if (isset($_POST['gate_colour'])) {

                        $enquary->gate_colour = $_POST['gate_colour'];
                    }
                    if (isset($_POST['wall_colour'])) {

                        $enquary->wall_colour = $_POST['wall_colour'];
                    }

                    if (isset($_POST['clint_name'])) {

                        $enquary->clint_name = $_POST['clint_name'];
                    }
                    if (isset($_POST['applied_for_loan'])) {

                        $enquary->applied_for_loan = $_POST['applied_for_loan'];
                    }

                    if (isset($_POST['taotal_family_mamber'])) {

                        $enquary->taotal_family_mamber = $_POST['taotal_family_mamber'];
                    }

                    if (isset($_POST['earning_mamber'])) {

                        $enquary->earning_mamber = $_POST['earning_mamber'];
                    }

                    if (isset($_POST['depandent'])) {

                        $enquary->depandent = $_POST['depandent'];
                    }
                    if (isset($_POST['resident_detail'])) {

                        $enquary->resident_detail = $_POST['resident_detail'];
                    }
                    if (isset($_POST['neighbor'])) {

                        $enquary->neighbor = $_POST['neighbor'];
                    }
                    if (isset($_POST['time_of_job'])) {

                        $enquary->time_of_job = $_POST['time_of_job'];
                    }

                    if (isset($_POST['total_emp'])) {

                        $enquary->total_emp = $_POST['total_emp'];
                    }

                    if (isset($_POST['net_sailry'])) {

                        $enquary->net_sailry = $_POST['net_sailry'];
                    }
                    ////////////////////////////////////////////////////////////////////
                    ////////////////////////////

                    if (isset($_FILES['office_image']['name'])) {

                        $uploadeDir = YiiBase::getPathOfAlias('webroot') . config::getUploadDir();
                        //office image
                        $office_image = $_FILES['office_image']['name'];
                        $office_imagefile = pathinfo($office_image);
                        $ext = $office_imagefile['extension'];
                        $fileName = guid() . '.' . $ext;
                        $uploadFileName = $uploadeDir . $fileName;

                        if (!empty($_FILES['office_image']['tmp_name'])) {
                            $up = move_uploaded_file($_FILES['office_image']['tmp_name'], $uploadFileName);
                            if ($up) {
                                $morethumb = createThumb($uploadFileName, UPLOADED_PHOTO_THUMB_WIDTH, UPLOADED_PHOTO_THUMB_HEIGHT, true, $fileName);
                            }
                            $enquary->setAttribute('office_image', $fileName);
                        };
                    }

                    if (isset($_FILES['person_image']['name'])) {

                        $person_image = $_FILES['person_image']['name'];
                        $person_imagefile = pathinfo($person_image);
                        $ext = $person_imagefile['extension'];
                        $fileName1 = guid() . '.' . $ext;
                        $uploadFileName1 = $uploadeDir . $fileName1;

                        if (!empty($_FILES['person_image']['tmp_name'])) {
                            $up1 = move_uploaded_file($_FILES['person_image']['tmp_name'], $uploadFileName1);
                            if ($up1) {
                                $morethumb = createThumb($uploadFileName1, UPLOADED_PHOTO_THUMB_WIDTH, UPLOADED_PHOTO_THUMB_HEIGHT, true, $fileName1);
                            }
                            $enquary->setAttribute('person_image', $fileName1);
                        }
                    }
                    if (isset($_FILES['poi_image']['name'])) {

                        $poi_image = $_FILES['poi_image']['name'];
                        $poi_imagefile = pathinfo($poi_image);
                        $ext = $poi_imagefile['extension'];
                        $fileName2 = guid() . '.' . $ext;
                        $uploadFileName2 = $uploadeDir . $fileName2;

                        if (!empty($_FILES['poi_image']['tmp_name'])) {
                            $up2 = move_uploaded_file($_FILES['poi_image']['tmp_name'], $uploadFileName1);
                            if ($up2) {
                                $morethumb = createThumb($uploadFileName2, UPLOADED_PHOTO_THUMB_WIDTH, UPLOADED_PHOTO_THUMB_HEIGHT, true, $fileName2);
                            }
                            $enquary->setAttribute('poi_image', $fileName2);
                        }
                    }
                    if (isset($_FILES['poa_image']['name'])) {

                        $poa_image = $_FILES['poa_image']['name'];
                        $poa_imagefile = pathinfo($poi_image);
                        $ext3 = $poa_imagefile['extension'];
                        $fileName3 = guid() . '.' . $ext3;
                        $uploadFileName3 = $uploadeDir . $fileName3;

                        if (!empty($_FILES['poa_image']['tmp_name'])) {
                            $up3 = move_uploaded_file($_FILES['poa_image']['tmp_name'], $uploadFileName3);
                            if ($up3) {
                                $morethumb = createThumb($uploadFileName3, UPLOADED_PHOTO_THUMB_WIDTH, UPLOADED_PHOTO_THUMB_HEIGHT, true, $fileName3);
                            }
                            $enquary->setAttribute('poa_image', $fileName3);
                        }
                    }
                    if (isset($_FILES['signature']['name'])) {
                        $signature = $_FILES['signature']['name'];
                        $signaturefile = pathinfo($signature);
                        $ext4 = $signaturefile['extension'];
                        $fileName4 = guid() . '.' . $ext4;
                        $uploadFileName4 = $uploadeDir . $fileName4;

                        if (!empty($_FILES['signature']['tmp_name'])) {
                            $up4 = move_uploaded_file($_FILES['signature']['tmp_name'], $uploadFileName4);
                            if ($up3) {
                                $morethumb = createThumb($uploadFileName4, UPLOADED_PHOTO_THUMB_WIDTH, UPLOADED_PHOTO_THUMB_HEIGHT, true, $fileName4);
                            }
                            $enquary->setAttribute('signature', $fileName4);
                        }
                    }


                    $enquary->save();

                    $data = new ECOMResult(200, ' Enquary verified successfully.');
                } else {
                    $data = new ECOMResult(501, 'Enquary Id not valid.');
                }
            } else {
                $data = new ECOMResult(501, 'Enquary Id can not be blank.');
            }



            ECOMApiResponder::Respond($data, date('m-d-Y'));
        }
    }

    /* --------------- End POST Actions --------------------- */
}

?>
